<layout name="Layout/main" />
<div class="panel panel-default">
	<div class="panel-heading">
		<span class='glyphicon glyphicon-volume-up text-danger'></span><span
			class='alert-tip text-primary' style='padding-left: 10px;'>定期备份可以保护数据安全！</span>
	</div>
	<!-- Table -->
	<form action="{:U('sys/backup')}" method='post' id='defaultForm'>
		<table class="table  panel-body ">

			<tr>
				<th>表名称</th>
				<th>表引擎</th>
				<th>表记录</th>
				<th>注释</th>
				<th>备份状态</th>
				<th>操作</th>
			</tr>
			<empty name='list'>
			<tr>
				<td colspan='5'>暂无数据</td>
			</tr>
			<else /> <volist name="list" id='vo'>
			<tr>
				<td><input type="checkbox" name='tables[]' value="{$vo.name}" />
					{$vo.name}</td>
				<td>{$vo.engine}</td>
				<td>{$vo.rows}</td>
				<td>{$vo.comment}</td>
				<td class='infos'>未备份</td>
				<td><a class='btn btn-xs text-danger'
					href="{:U('sys/repair',array('tables'=>$vo['name']))}" title="修复表 "><span
						class="glyphicon glyphicon-wrench"></span> 修复</a> <a
					class='btn  btn-xs ' href="{:U('sys/optimize',array('tables'=>$vo['name']))}"
					title="优化表"><span
						class="glyphicon glyphicon-pushpin text-primary"></span> 优化</a></td>
			</tr>
			</volist>
			<tr>
				<td colspan='5'><span class='btn btn-success btn-sm'><input
						type='checkbox' id='backup'> 全选</span>
					<button class='btn btn-info btn-sm' id='baif' type='submit'>备份</button>
					<span> <a href="{:U('sys/optimize')}" id='optimize'
						class='btn btn-danger btn-sm'>优化</a></span>
						
						<span> <a href="{:U('sys/optimize')}" id='repair'
						class='btn btn-danger btn-sm'>修复</a></span>
						</td>

			</tr>

			</empty>
		</table>
	</form>
	<script>
		$(function() {
			$('#backup').on('click', function() {
				T.sAllNode('tables[]');
			})

		})
	</script>
</div>
<block name="script"> <script type="text/javascript">
	(
					function($) {
						var $form = $("#defaultForm"), $export = $("#baif"), $optimize = $("#optimize"), $repair = $("#repair");
						//优化
						$optimize.click(function() {
                            $('.alert-tip').html("正在优化数据表，请不要关闭页面...");
							$.post(this.href, $form.serialize(),
									function(data) {
								    $('.alert-tip').html("本次优化完成...");
									//$.messager.alert('操作提示', data.info);

									}, "json");
							return false;
						});
                     //修复
                     	$repair.click(function() {
                            $('.alert-tip').html("正在修复数据表，请不要关闭页面...");
							$.post(this.href, $form.serialize(),
									function(data) {
								    $('.alert-tip').html("本次修复完成...");
									//$.messager.alert('操作提示', data.info);

									}, "json");
							return false;
						});
						$export.click(function() {
									$export.parent().children('a').addClass(
											"disabled");
									// $export.html("正在发送备份请求...");
									$('.alert-tip').html(" 正在发送备份请求...");
									$
											.post(
													$form.attr("action"),
													$form.serialize(),
													function(data) {
														if (data.status) {
															tables = data.tables;
															//$export.html(data.info + "开始备份，请不要关闭本页面！");
															$('.alert-tip')
																	.html(data.info+ "开始备份，请不要关闭本页面！");
															backup(data.tab);
															window.onbeforeunload = function() {
																return "正在备份数据库，请不要关闭！"
															}
														} else {
															$.messager.alert('操作提示', data.info);
															$export.parent().children('a').removeClass(
"disabled");
															// $export.html("立即备份");
															$('.alert-tip').html("立即备份");
														}
													}, "json");
									return false;
								});

						function backup(tab, status) {
							status && showmsg(tab.id, "开始备份...(0%)");
							$.get($form.attr("action"), tab, function(data) {
								if (data.status) {
									showmsg(tab.id, data.info);

									if (!$.isPlainObject(data.tab)) {
										$export.parent().children('a')
												.removeClass("disabled");
										//$export.html("备份完成，点击重新备份");
										$('.alert-tip').html("本次备份已经完成！");
										window.onbeforeunload = function() {
											return null
										}
										return;
									}
									backup(data.tab, tab.id != data.tab.id);
								} else {
									$.messager.alert('操作提示', data.info);
									//updateAlert(data.info, 'alert-error');
									$export.parent().children().removeClass(
											"disabled");
									//$export.html("立即备份");
									$('.alert-tip').html("立即备份");
								}
							}, "json");

						}

						function showmsg(id, msg) {
							$form.find("input[value=" + tables[id] + "]")
									.closest("tr").find(".infos").html(msg);
						}
					})(jQuery);
</script> </block>